package com.xuyiwei.xblog.modular.system.controller;

import com.xuyiwei.xblog.core.base.controller.BaseController;
import com.xuyiwei.xblog.core.base.response.Result;
import com.xuyiwei.xblog.core.exception.BizExceptionEnum;
import com.xuyiwei.xblog.core.exception.GunsException;
import com.xuyiwei.xblog.core.util.ToolUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.xuyiwei.xblog.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.xuyiwei.xblog.business.system.model.Config;
import com.xuyiwei.xblog.business.system.service.IConfigService;
import java.util.List;

/**
 * 系统配置控制器
 *
 * @author fengshuonan
 * @Date 2018-10-28 17:34:19
 */
@Controller
@RequestMapping("/system/config")
public class ConfigController extends BaseController {

    private String PREFIX = "/system/config/";

    @Autowired
    private IConfigService configService;

    /**
     * 跳转到系统配置首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "config.html";
    }

    /**
     * 跳转到添加系统配置
     */
    @RequestMapping("/config_add")
    public String configAdd() {
        return PREFIX + "config_add.html";
    }

    /**
     * 跳转到修改系统配置
     */
    @RequestMapping("/config_update/configId}")
    public String configUpdate(@PathVariable Integer configId, Model model) {
        Config config = configService.selectById(configId);
        model.addAttribute("item",config);
        LogObjectHolder.me().set(config);
        return PREFIX + "config_edit.html";
    }

    /**
     * 获取系统配置列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Result<List<Config>> list(String condition) {
        Result<List<Config>> result = Result.instance();
        try{
            List<Config> list = configService.selectList(null);
            result.setData(list);
        }catch (Exception e){
            result.setError("500","获取文章标签出错");
        }
        return result;
    }

    /**
     * 新增系统配置
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Result<Boolean> add(Config config) {
        Result<Boolean> result = Result.instance();
        try{
            configService.insert(config);
            result.setData(true);
        }catch (Exception e){
            result.setError(BizExceptionEnum.REQUEST_NULL);
        }
        return result;
    }

    /**
     * 删除系统配置
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Result<Boolean> delete(@RequestParam(required = true, value = "ids[]") List<Integer> ids) {
        Result<Boolean> result = Result.instance();
        try{
            if (ToolUtil.isEmpty(ids)) {
                throw new GunsException(BizExceptionEnum.REQUEST_NULL);
            }
            this.configService.deleteBatchIds(ids);
        }catch (Exception e){
            result.setError(BizExceptionEnum.REQUEST_NULL);
        }
        return result;
    }

    /**
     * 修改系统配置
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Result<Boolean> update(Config config) {
        Result<Boolean> result = Result.instance();
        try{
            configService.updateById(config);
        }catch (Exception e){
            result.setError(BizExceptionEnum.REQUEST_NULL);
        }
        return result;
    }

    /**
     * 系统配置详情
     */
    @RequestMapping(value = "/detail/configId}")
    @ResponseBody
    public Result<Config> detail(@PathVariable("configId") Integer configId) {
        Result<Config> result = Result.instance();
        try{
            Config config = configService.selectById(configId);
            result.setData(config);
        }catch (Exception e){
            result.setError(BizExceptionEnum.REQUEST_NULL);
        }
        return result;
    }

    /**
     * 获取系统配置列表
     */
    @RequestMapping(value = "/getOne")
    @ResponseBody
    public Result<Config> getOne() {
        Result<Config> result = Result.instance();
        try{
            Config config = configService.get();
            result.setData(config);
        }catch (Exception e){
            result.setError("500","获取文章标签出错");
        }
        return result;
    }
}
